package com.xuxin.nantai.activity;

import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.nantai.R;
import com.xuxin.nantai.base.BaseActivity;
import com.xuxin.nantai.bean.LoginBean;
import com.xuxin.nantai.contract.LoginContract;
import com.xuxin.nantai.presenter.LoginPresenter;
import com.xuxin.nantai.utils.ACache;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.06.05 0005
 */
public class LoginActivity extends BaseActivity implements LoginContract.View {
    private LoginContract.Presenter presenter = new LoginPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;

    @BindView(R.id.login_remember)
    CheckBox login_remember;

    @BindView(R.id.login_name)
    EditText login_name;
    @BindView(R.id.login_password)
    EditText login_password;

    private MaterialDialog materialDialog;
    private String user, password;//用户名密码

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    public void initView() {
        title_name.setText("南泰质检登录");
        title_left.setVisibility(View.INVISIBLE);
        String LoginName = ACache.get(this).getAsString("LoginName");
        String password = ACache.get(this).getAsString("password");
        if (!StringUtils.isEmpty(LoginName)) {
            login_name.setText(LoginName);
        }
        if (!StringUtils.isEmpty(password)) {
            login_remember.setChecked(true);
            login_password.setText(password);
        }
    }

    @Override
    public void initData() {
    }

    @Override
    public void onComplete() {
        if (null != materialDialog) {
            materialDialog.dismiss();
        }
    }

    @Override
    @OnClick({R.id.login, R.id.login_change_password})
    public void onClick(View view) {
        KeyboardUtils.hideSoftInput(this);
        switch (view.getId()) {
            case R.id.login:
                user = login_name.getText().toString();
                password = login_password.getText().toString();
                if (StringUtils.isEmpty(user)) {
                    ToastUtils.showShort("请输入登录账号");
                } else if (StringUtils.isEmpty(password)) {
                    ToastUtils.showShort("请输入密码");
                } else {
                    handler.sendEmptyMessage(0);
                }
                break;
            case R.id.login_change_password:
                ActivityUtils.startActivity(PasswordActivity.class);
                break;
        }
    }

    public Handler handler = new Handler(message -> {
        switch (message.what) {
            case 0:
                materialDialog = new MaterialDialog.Builder(this)
                        .content("登陆中...")
                        .progress(true, 0)
                        .show();
                presenter.login(user, password);
                break;
        }
        return false;
    });

    @Override
    public void onSuccess(LoginBean loginBean) {
        ACache.get(this).put("LoginName", loginBean.getData().getLoginName());
        ACache.get(this).put("UserName", loginBean.getData().getUserName());
        if (login_remember.isChecked()) {
            ACache.get(this).put("password", password);
        } else {
            ACache.get(this).remove("password");
        }
        ActivityUtils.startActivity(MainActivity.class);
        finish();
    }
}